package net.solvetheriddle.wwclient;

import net.solvetheriddle.wwclient.database.WWDatabaseManager;
import net.solvetheriddle.wwclient.entity.Work;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class ShutDownReciever extends BroadcastReceiver {

    private static final String UNEXPECTED_CLOSE = "WW Client - činnost ukončena vypnutím telefonu";
    private WWDatabaseManager db;

    @Override
    public void onReceive(Context context, Intent intent) {
        db = new WWDatabaseManager(context);
        Work workToBeEnded = db.getUnfinishedWork();
        if (workToBeEnded != null) {
            workToBeEnded.setDescription(UNEXPECTED_CLOSE);
            workToBeEnded.setTo(WWLogic.getCurrentMillis());
            db.updateWork(workToBeEnded);
            WWLogic.finishCurrentWork();
        }
        db.close();
    }
}
